<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      class MyPromise {
        static allSettled(arr) {
          return new Promise((resolve) => {
            let resArr = new Array(arr.length);
            let count = 0;
            arr.forEach((item, key) => {
              item.then(
                (res) => {
                  let obj = {
                    status: "fulfilled",
                    value: res,
                  };

                  resArr[key] = obj;
                  count++;
                  if (count === arr.length) {
                    resolve(resArr);
                  }
                },
                (err) => {
                  let obj = {
                    status: "rejected",
                    reason: err,
                  };
                  resArr[key] = obj;
                  count++;
                  if (count === arr.length) {
                    resolve(resArr);
                  }
                }
              );
            });
          });
        }
      }

      let p1 = new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve(1111);
        }, 1000);
      });

      let p2 = new Promise((resolve, reject) => {
        setTimeout(() => {
          // resolve(2222);
          reject("222error");
        }, 2000);
      });

      let p3 = new Promise((resolve, reject) => {
        setTimeout(() => {
          // resolve(3333);
          reject("333error");
        }, 1000);
      });

      MyPromise.allSettled([p1, p2, p3]).then((res) => {
        console.log(res);
      });
    </script>
  </body>
</html>
